package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.SecondTestBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class SecondRuleActivity extends BaseActivity {

    String url, userid;
    String testid;
    UserPresenter mUserPresenter;
    UserBean.DataEntity dataEntity;
    Intent intent;

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.second_rule)
    WebView mSecondRule;
    @BindColor(R.color.white)
    int white;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_rule);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        mUserPresenter = new UserPresenterImp();
        dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        initToolbar();
        initIntent();
        initWebView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mSecondRule.loadUrl(url);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    private void initWebView() {
        mSecondRule.loadUrl(url);
        mSecondRule.getSettings().setJavaScriptEnabled(true);
        mSecondRule.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                dialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dialog.dismiss();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                dataEntity.readInfoFromLocal(SecondRuleActivity.this);
                if (url.contains("http://match")) {
                    if (TextUtils.isEmpty(dataEntity.getUserid())) {
                        Intent intent = new Intent(SecondRuleActivity.this, LoginAcivity.class);
                        intent.putExtra(Constant.ACTIVITY_TO_LOGIN, 102);
                        startActivity(intent);
                    } else {
//                        mUserPresenter.secondEnter(dataEntity.getUserid());
//                        mUserPresenter.secondTest(dataEntity.getUserid());
                    }
                    return true;
                } else if (url.contains("http://rank")) {
                    Intent intent = new Intent(SecondRuleActivity.this, RankActivity.class);
                    intent.putExtra(Constant.TO_RANK_LIBID, url.substring(12));
                    intent.putExtra(Constant.TO_TEST_RANK_IF_SECOND, true);
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });
    }

//    public void onEventMainThread(SecondEnterBean bean) {
//        if (bean.getReturn_code() == 0) {
//            testid = bean.getTestid();
//            intent.putExtra(Constant.TO_SECOND_RULE_TEST_ID, testid);
//            DeviceUtil.Log("secondEnter O = " + bean.getMsg());
//            startActivity(intent);
//            finish();
//        } else {
//            DeviceUtil.Log("secondEnter !0 = " + bean.getMsg());
//            DeviceUtil.MyToast(this, bean.getMsg());
//            Intent intent = new Intent(this, MainActivity.class);
//            startActivity(intent);
//        }
//    }

    public void onEventMainThread(SecondTestBean bean) {
//        DeviceUtil.Log("now = " + System.currentTimeMillis() + " start = " + bean.getStart() + " end = " + bean.getEnd());
//        if (bean.getReturn_code() == 0) {
//            if (Long.parseLong(bean.getStart()) * 1000 < System.currentTimeMillis() &
//                    Long.parseLong(bean.getEnd()) * 1000 > System.currentTimeMillis()) {
//                intent = new Intent(this, ReadyTestActivity.class);
//                intent.putExtra(Constant.TO_READY_FIRST_TEST_ID, String.valueOf(bean.getId()));
//                intent.putExtra(Constant.TO_SECOND_RULE_IS_SECOND_TEST, true);
//                intent.putExtra(Constant.TO_SECOND_RULE_END_TIME, bean.getEnd());
//                startActivity(intent);
//                finish();
////                mUserPresenter.secondEnter(userid);
//            } else if (Long.parseLong(bean.getStart()) * 1000 > System.currentTimeMillis()) {
//                DeviceUtil.MyToast(this, "复赛时间未到（8月8日20:00-20:30）");
//            } else if (Long.parseLong(bean.getEnd()) * 1000 < System.currentTimeMillis()) {
//                DeviceUtil.MyToast(this, "复赛时间已过");
//            }
//        } else {
//            DeviceUtil.MyToast(this, bean.getMsg());
//        }
    }

    private void initIntent() {
        Intent intent = getIntent();
        url = intent.getStringExtra(Constant.TO_SECOND_RULE);
    }

    private void initToolbar() {
        mToolbar.setTitle("\"金轮-蓝钻杯\"全国梳理知识大赛");
        mToolbar.setTitleTextColor(white);
        mToolbar.setNavigationIcon(R.mipmap.btn_exit);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }

}
